package site.marqstree.kotlin.library.good.ui.fragment.category.list

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import kotlinx.android.synthetic.main.fragment_category_list.*
import site.marqstree.kotlin.library.base.ui.fragment.BaseMvpFragment
import site.marqstree.kotlin.library.good.R
import site.marqstree.kotlin.library.good.ui.fragment.category.list.bean.CategoryBean
import site.marqstree.kotlin.library.good.ui.fragment.category.list.inject.DaggerCategoryListComponent
import site.marqstree.kotlin.library.good.ui.fragment.category.list.listener.ICategoryList
import site.marqstree.kotlin.library.good.ui.fragment.category.list.presenter.CategoryListPresenter
import site.marqstree.kotlin.library.good.ui.fragment.category.list.recyclerview.CategoryListAdapter
import site.marqstree.kotlin.library.good.ui.fragment.category.list.view.CategoryListView


/*
 * 项目名: KotlinMall
 * 包名: site.marqstree.kotlin.library.good.ui.fragment.category.list
 * 文件名: CategoryListFragment
 * 创建者: marqstree
 * 创建时间: 2020/3/6 15:30
 * 描述: 商品分类列表
 */
class CategoryListFragment(): BaseMvpFragment<CategoryListPresenter>(),
    CategoryListView {

    lateinit var mListener: ICategoryList

    private lateinit var mListAdapter: CategoryListAdapter

    private lateinit var mCategoryList: MutableList<CategoryBean>

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        super.onCreateView(inflater, container, savedInstanceState)
        return inflater.inflate(R.layout.fragment_category_list,null)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initCategoryList()
        mPresenter.getCategoryList()
    }

    fun getCurrentCategoryId():Int{
        return mListAdapter.getCurrentCategoryId()
    }

    private fun initCategoryList() {
        val manager = LinearLayoutManager(context)
        mRvCategoryList.layoutManager = manager
    }

    override fun initBusinessInjection() {
        DaggerCategoryListComponent.builder()
            .activityComponent(activityComponent)
            .build()
            .inject(this)
        mPresenter.mView = this
    }

    override fun onCategoryListResult(list: List<CategoryBean>) {
        mCategoryList = list.toMutableList()
        mCategoryList.get(0).isSelected = true
        mListAdapter = CategoryListAdapter(
                            mCategoryList,
                            mListener)
        mRvCategoryList.adapter = mListAdapter
    }
}